using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qGSCOLOR = from m in mtemp.GSCOLOR select m;
       DBMapper.MapGSCOLOR(qGSCOLOR.ToList(), true);
       */
        public List<GSCOLOR> MapGSCOLOR(XElement item2, bool mergedb)
        {
            List<GSCOLOR> result = new List<GSCOLOR>();
            var item4 = from item3 in item2.Elements()
                        select new GSCOLOR
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            GSCMANDANT = (string)item3.Element("GSCMANDANT"),
                            GSCART = (string)item3.Element("GSCART"),
                            GSCVAR = (string)item3.Element("GSCVAR"),
                            GSCFARBE = (string)item3.Element("GSCFARBE"),
                            GSCBEZ = (string)item3.Element("GSCBEZ"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapGSCOLOR(result, mergedb);
          return result;
        }
        ///DBMapper.MapGSCOLOR(mtemp.GSCOLOR.ToList<GSCOLOR>(), true);        
       public void MapGSCOLOR(List<GSCOLOR> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from GSCOLOR row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.GSCOLOR.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.GSCOLOR.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.GSCOLOR.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "GSCOLOR")]
    public class GSCOLOR : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _GSCMANDANT="0";
        [Column]
        public string GSCMANDANT
        {
            get
            {
                return _GSCMANDANT;
            }
            set
            {
                if (value != _GSCMANDANT)
                {
                     _GSCMANDANT = value;
                    NotifyPropertyChanged("GSCMANDANT");
                }
            }
        }
        private string  _GSCART="0";
        [Column]
        public string GSCART
        {
            get
            {
                return _GSCART;
            }
            set
            {
                if (value != _GSCART)
                {
                     _GSCART = value;
                    NotifyPropertyChanged("GSCART");
                }
            }
        }
        private string  _GSCVAR="0";
        [Column]
        public string GSCVAR
        {
            get
            {
                return _GSCVAR;
            }
            set
            {
                if (value != _GSCVAR)
                {
                     _GSCVAR = value;
                    NotifyPropertyChanged("GSCVAR");
                }
            }
        }
        private string  _GSCFARBE="0";
        [Column]
        public string GSCFARBE
        {
            get
            {
                return _GSCFARBE;
            }
            set
            {
                if (value != _GSCFARBE)
                {
                     _GSCFARBE = value;
                    NotifyPropertyChanged("GSCFARBE");
                }
            }
        }
        private string  _GSCBEZ="0";
        [Column]
        public string GSCBEZ
        {
            get
            {
                return _GSCBEZ;
            }
            set
            {
                if (value != _GSCBEZ)
                {
                     _GSCBEZ = value;
                    NotifyPropertyChanged("GSCBEZ");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<GSCOLOR> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\GSCOLOR\\GSCOLOR" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\GSCOLOR" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("GSCOLOR");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (GSCOLOR row in ListInput)
                {
                    XElement f = new XElement("GSCOLOR");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement GSCMANDANT = new XElement("GSCMANDANT");
                         GSCMANDANT.Value = row.GSCMANDANT;
                          f.Add(GSCMANDANT);
                        XElement GSCART = new XElement("GSCART");
                         GSCART.Value = row.GSCART;
                          f.Add(GSCART);
                        XElement GSCVAR = new XElement("GSCVAR");
                         GSCVAR.Value = row.GSCVAR;
                          f.Add(GSCVAR);
                        XElement GSCFARBE = new XElement("GSCFARBE");
                         GSCFARBE.Value = row.GSCFARBE;
                          f.Add(GSCFARBE);
                        XElement GSCBEZ = new XElement("GSCBEZ");
                         GSCBEZ.Value = row.GSCBEZ;
                          f.Add(GSCBEZ);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
